/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author user
 */
public class roomBean {

    /**
     *
     */
    private String name = null;
    
    /**
     *
     */
    private String owner = null;

    /**
     *
     */
    private String parent = null;

    private boolean  isPersistent = false;
    
    /**
     * 
     */
    private Map users = new HashMap();

    /**
     *
     */
    private List chats = new LinkedList();

    /**
     *
     */
    private int chatLength = 30;
    
 

    public roomBean(String name, String owner, String Parent) {
        this.name = name;
        this.owner = owner;
        this.parent = parent;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

        /**
     * @return the chatLength
     */
    public int getChatLength() {
        return chatLength;
    }

    /**
     * @param chatLength the chatLength to set
     */
    public void setChatLength(int chatLength) {
        this.chatLength = chatLength;
    }

    /**
     *
     * @param user
     */
    public synchronized void addUser(UserBean user) {
        users.put(user.getName(), user);
    }

    /**
     *
     * @param name
     * @return
     */
    public synchronized  Object removeUser(String name) {
        return users.remove(name);
    }

    /**
     *
     * @param name
     * @return
     */
    public UserBean getUser (String name) {
        return (UserBean) users.get(name);
    }

    /**
     *
     * @param name
     * @return
     */
    public boolean isUsereExist (String name) {
        return users.containsKey(name);
    }

    /**
     *
     * @return
     */
    public int getUsersSize() {
        return users.size();
    }

    /**
     *
     * @return
     */
    public Set getUsersInMap()
    {
        return users.entrySet();
    }


    /**
     * @return the owner
     */
    public String getOwner() {
        return owner;
    }

    /**
     * @return the parent
     */
    public String getParent() {
        return parent;
    }


    /**
     *
     * @return
     */
    public UserBean[] getUsersInArray() {
        Set Users = getUsersInMap();
        UserBean[] userArray = new UserBean[Users.size()];
        Iterator it = Users.iterator();
        int i = 0;
        while(it.hasNext()) {
            Map.Entry me = (Map.Entry) it.next();
            String key = (String) me.getKey();
            userArray[i] = (UserBean) me.getValue();
            ++i;
        }
        return userArray;
    }

    /**
     *
     * @param chat
     */
    public synchronized void addChat(chatBean chat) {
        if (chats.size() == getChatLength()) {
            ((LinkedList)chats).removeFirst();
        }
        chats.add(chat);
    }

    /**
     *
     * @return
     */
    public ListIterator getChats() {
        return chats.listIterator();
    }

    /**
     *
     * @param time
     * @return
     */
    public chatBean[] getChats(long time) {
        ListIterator lit = getChats();
        List list = new ArrayList();
        chatBean chat;
        while (lit.hasNext()) {
            chat = (chatBean) lit.next();
            if (chat.getTime()>= time) {
                list.add(chat);
            }
        }

        Object os[] = list.toArray();
        chatBean[] chatlist = new chatBean[os.length];
        for (int i = 0; i < os.length; ++i) {
            chatlist[i] = (chatBean) os[i];
        }
        return chatlist;
    }

    /**
     * @param parent the parent to set
     */
    public void setParent(String parent) {
        this.parent = parent;
    }

    /**
     * @return the isPersistent
     */
    public boolean isIsPersistent() {
        return isPersistent;
    }

    /**
     * @param isPersistent the isPersistent to set
     */
    public void setIsPersistent(boolean isPersistent) {
        this.isPersistent = isPersistent;
    }

}
